package com.eascs.workflow.email.service.impl;

import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.eascs.notify.constant.ModelResult;
import com.eascs.notify.wechat.WechatNotifyRequest;
import com.eascs.notify.wechat.WechatNotifyResponse;
import com.eascs.workflow.common.entity.sys.WfWechat;
import com.eascs.workflow.email.dao.SysSendEmailOrMessageDao;
import com.eascs.workflow.email.service.WechatNotifierService;
import com.eascs.workflow.email.util.WeixinUtil;

@Service
public class WechatNotifierServiceImpl implements WechatNotifierService {
	@Autowired
	private SysSendEmailOrMessageDao dao;

	@Override
	public ModelResult<WechatNotifyResponse> notify(WechatNotifyRequest wechatNotifyRequest) {
		String POST_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN";

		ModelResult<WechatNotifyResponse> model = new ModelResult<WechatNotifyResponse>();
		if (StringUtils.isBlank(wechatNotifyRequest.getCorpId())) {

			return model.withError("非空错误", "corpId为空！");
		}
		if (StringUtils.isBlank(wechatNotifyRequest.getSecret())) {

			return model.withError("非空错误", "secret为空！");
		}

		if (StringUtils.isBlank(wechatNotifyRequest.getMsgType())) {

			return model.withError("非空错误", "msgType为空！");
		}

		if (wechatNotifyRequest.getMsgType().equals("text") && StringUtils.isBlank(wechatNotifyRequest.getContent())) {

			return model.withError("非空错误", "消息内容为空！");
		}

		if (!wechatNotifyRequest.getMsgType().equals("text") && !wechatNotifyRequest.getMsgType().equals("news") && StringUtils.isBlank(wechatNotifyRequest.getMediaId())) {

			return model.withError("非空错误", "media_id为空！");
		}

		String access_token = WeixinUtil.getAccessToken(wechatNotifyRequest.getCorpId(), wechatNotifyRequest.getSecret()).getToken(); // 获取token
		StringBuffer sb = new StringBuffer();
		// 发送消息url地址
		sb.append("{");
		sb.append("\"touser\":" + "\"" + wechatNotifyRequest.getTouser() + "\",");
		sb.append("\"toparty\":" + "\"" + wechatNotifyRequest.getToparty() + "\",");
		sb.append("\"totag\":" + "\"" + wechatNotifyRequest.getTotag() + "\",");
		sb.append("\"msgtype\":" + "\"" + wechatNotifyRequest.getMsgType() + "\",");
		sb.append("\"agentid\":" + "\"" + wechatNotifyRequest.getAgentid() + "\",");
		if (wechatNotifyRequest.getMsgType().equals("text")) {
			sb.append("\"text\":" + "{");
			sb.append("\"content\":" + "\"" + wechatNotifyRequest.getContent() + "\"");
			sb.append("},");
		} else if (wechatNotifyRequest.getMsgType().equals("image")) {
			sb.append("\"image\":" + "{");
			sb.append("\"media_id\":" + "\"" + wechatNotifyRequest.getMediaId() + "\"");
			sb.append("},");
		} else if (wechatNotifyRequest.getMsgType().equals("voice")) {
			sb.append("\"voice\":" + "{");
			sb.append("\"media_id\":" + "\"" + wechatNotifyRequest.getMediaId() + "\"");
			sb.append("},");
		} else if (wechatNotifyRequest.getMsgType().equals("video")) {
			sb.append("\"video\":" + "{");
			sb.append("\"media_id\":" + "\"" + wechatNotifyRequest.getMediaId() + "\",");
			sb.append("\"title\":" + "\"" + wechatNotifyRequest.getTitle() + "\",");
			sb.append("\"description\":" + "\"" + wechatNotifyRequest.getDescription() + "\"");
			sb.append("},");
		} else if (wechatNotifyRequest.getMsgType().equals("file")) {
			sb.append("\"file\":" + "{");
			sb.append("\"media_id\":" + "\"" + wechatNotifyRequest.getMediaId() + "\"");
			sb.append("},");
		} else if (wechatNotifyRequest.getMsgType().equals("news")) {
			sb.append("\"news\":" + "{");
			sb.append("\"articles\":" + "[");
			sb.append("{");
			sb.append("\"title\":" + "\"" + wechatNotifyRequest.getTitle() + "\",");
			sb.append("\"description\":" + "\"" + wechatNotifyRequest.getDescription() + "\",");
			sb.append("\"url\":" + "\"" + wechatNotifyRequest.getUrl() + "\",");
			sb.append("\"picurl\":" + "\"" + wechatNotifyRequest.getPicurl() + "\"");
			sb.append("}");
			sb.append("]");
			sb.append("},");
		}
		sb.append("\"safe\":" + "\"" + wechatNotifyRequest.getSafe() + "\"");
		sb.append("}");

		String PostData = String.format(sb.toString());
		int result = WeixinUtil.PostMessage(access_token, "POST", POST_URL, PostData);
		WfWechat wf = new WfWechat();
		wf.setRequestId(wechatNotifyRequest.getRequestId());
		wf.setCorpId(wechatNotifyRequest.getCorpId());
		wf.setSecret(wechatNotifyRequest.getSecret());
		wf.setMsgType(wechatNotifyRequest.getMsgType());
		wf.setAgentid(wechatNotifyRequest.getAgentid());
		wf.setTouser(StringUtils.isEmpty(wechatNotifyRequest.getTouser()) ? "@all" : wechatNotifyRequest.getTouser());
		wf.setToparty(wechatNotifyRequest.getToparty());
		wf.setTotag(wechatNotifyRequest.getTotag());
		wf.setContent(wechatNotifyRequest.getContent());
		wf.setMediaId(wechatNotifyRequest.getMediaId());
		wf.setTitle(wechatNotifyRequest.getTitle());
		wf.setDescription(wechatNotifyRequest.getDescription());
		wf.setUrl(wechatNotifyRequest.getUrl());
		wf.setPicurl(wechatNotifyRequest.getPicurl());
		wf.setSafe(StringUtils.isEmpty(wechatNotifyRequest.getSafe()) ? "0" : wechatNotifyRequest.getSafe());

		int i = 0;
		if (result == 0) {
			wf.setStatus(1);
			wf.setCreateTime(new Date());
			i = dao.insertWfWechat(wf);
		}
		if (i == 0) {
			return model.withError(SmsErrorDic.SQLERROR, "数据库插入异常");
		}
		WechatNotifyResponse wechatNotifyResponse = new WechatNotifyResponse();
		wechatNotifyResponse.setResult(result);
		wechatNotifyResponse.setResponseDateTime(new Date());
		model.setModel(wechatNotifyResponse);
		return model;

	}

}
